package com.situ.blog.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.situ.blog.pojo.Blog;
import com.situ.blog.pojo.VO.BlogVO;
import com.situ.blog.pojo.query.BlogQuery;
import com.situ.blog.service.IBlogService;
import com.situ.blog.util.Result;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author dell
 * @since 2024-09-20
 */
@RestController
@RequestMapping("/blog")
public class BlogController {

    @Autowired
    private IBlogService blogService;

    //Restful    GET(查找)、POST(添加)、DELETE(删除)、PUT(修改)
    //@RequestMapping("/list")
    //@RequestMapping(value = "/list", method = {RequestMethod.GET, RequestMethod.POST})
    //@RequestMapping(value = "/list", method = RequestMethod.GET)
    @GetMapping("/list")
    public Result list(BlogQuery blogQuery) {
        /*IPage<Blog> blogIPage = new Page<>(blogQuery.getPage(), blogQuery.getLimit());
        QueryWrapper<Blog> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(!ObjectUtils.isEmpty(blogQuery.getName()), "name", blogQuery.getName());
        queryWrapper.like(!ObjectUtils.isEmpty(blogQuery.getTypeId()),  "type_id", blogQuery.getTypeId());
        queryWrapper.between(!ObjectUtils.isEmpty(blogQuery.getBeginCreateTime()) && !ObjectUtils.isEmpty(blogQuery.getEndCreateTime()),
                "create_time", blogQuery.getBeginCreateTime(), blogQuery.getEndCreateTime());
        blogService.page(blogIPage, queryWrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("total", blogIPage.getTotal());
        map.put("list", blogIPage.getRecords());
        return Result.ok(map);*/

        IPage<BlogVO> blogIPage = new Page<>(blogQuery.getPage(), blogQuery.getLimit());
        IPage<BlogVO> page = blogService.selectByPage(blogIPage, blogQuery);
        Map<String, Object> map = new HashMap<>();
        map.put("total", page.getTotal());
        map.put("list", page.getRecords());
        return Result.ok(map);
    }

    // /admin/deleteById?id=1
    // /admin/deleteById/1
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(@PathVariable Integer id) {
        blogService.removeById(id);
        return Result.ok("删除成功");
    }

    // /admin/deleteById/1,2,3
    @DeleteMapping("/deleteAll/{ids}")
    public Result deleteAll(@PathVariable Integer[] ids) {
        blogService.removeByIds(Arrays.asList(ids));
        return Result.ok("删除成功");
    }

    // @RequestBody : 封装前台传递过来的JSON格式数据
    // @ResponseBody : 将数据转换为JSON格式返回
    @PostMapping("/add")
    public Result add(@RequestBody Blog blog) {
        blogService.save(blog);
        return Result.ok("添加成功");
    }

    @GetMapping("/selectById/{id}")
    public Result selectById(@PathVariable Integer id) {
        Blog blog = blogService.getById(id);
        return Result.ok(blog);
    }

    @PutMapping("/update")
    public Result update(@RequestBody Blog blog) {
        //blogService.updateById(blog);
        blogService.saveOrUpdate(blog);
        return Result.ok("更新成功");
    }

    @PutMapping("/updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Integer id, @PathVariable Integer status) {
        UpdateWrapper<Blog> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id).set("status", status);
        blogService.update(updateWrapper);
        return Result.ok("更新成功");
    }

    @GetMapping("/exportExcel")
    public void exportExcel(HttpServletResponse response) {
        blogService.exportExcel(response);
    }

    @RequestMapping("/importExcel")
    public Result importExcel(MultipartFile file) {
        blogService.importExcel(file);
        return Result.ok();
    }
}

